<?php
namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Request;

class Common extends Controller{

	public function _initialize(){
		parent::_initialize();
		
		$request = Request::instance();
		$_controller = strtolower($request->controller());
		$_action = strtolower($request->action());
		
		$inviterid = $request->param('inviterid','');//邀请人ID
		$invitelevels = $request->param('levels','');//邀请新人加入的目标代理等级
		$inviteagentid = $request->param('agent_id','');//邀请人的代理ID
		
		if(!IS_TEST){
			
			//当前登陆状态
			if(!Session::has('agent_id')){
				
				//判断是否从代理邀请的分享链接过来
				/*
/agent/invite_create.do?agent_id=AG_20200419214821465874&levels=AgentLevel_003,AgentLevel_004
/?inviterid=AG_20200429105115687821&levels=AgentLevel_003,AgentLevel_004
				*/
				if(
					$_controller == 'agent' && 
					$_action == 'invite_create' && 
					$inviteagentid != '' && 
					$invitelevels != ''
				){
					//受邀请
					__log('meidenglu shouyaoqin','sssss');
                    
                    $this->redirect('/?inviterid=' . $inviteagentid . '&levels=' . $invitelevels);
                    exit();
				}else{
					//不是受邀请
                    $t =  $_controller . '/' . $_action . '/' . $inviteagentid . '/' . $invitelevels;
                    __log($t,'sssss');
				}
				$this->redirect('/');
				exit();
			}else{
				//当前处于登陆状态
                defined('AGENT_ID') or define('AGENT_ID',Session::get('agent_id'));
				if(
					$_controller == 'agent' && 
					$_action == 'invite_create' && 
					$inviteagentid != '' && 
					$inviteagentid != AGENT_ID
				){
                    __log('denglule shouyaoqin','sssss');
					//受邀请
					$this->redirect('/?inviterid=' . $inviteagentid . '&levels=' . $invitelevels);
                    exit();
				}
			}
			
		}else{
			defined('AGENT_ID') or define('AGENT_ID',TEST_AGENT_ID);
			Session::set('agent_id',AGENT_ID);
		}
	}
	
	protected function J($list){
		$data = json_decode($list,true);
		if(!isset($data['flag'])){
			__log($list,'neterror');
			if(Request::instance()->isAjax()){
				ajax('网络错误');
			}
			exit('net error');
		}
		return $data;
	}

    public function index()
    {
		return view();
    }
	
}
